package com.loy.service;

import com.google.protobuf.ProtocolStringList;
import com.loy.entity.HelloProto;
import com.loy.entity.HelloServiceGrpc;
import io.grpc.stub.StreamObserver;

/**
 * 1.自定义实现了，实现自定义逻辑
 */
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {


    /**
     * 1. 接受客户端提交的参数
     * 2. 业务处理
     * 3. 提供返回值给客户端
     * @param request
     * @param responseObserver
     */
    @Override
    public void hello(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {

        // 1. 接受客户端提交的数据
        String name = request.getName();

        // 2. 业务处理
        System.out.println("nam = " + name);

        // 3. 封装响应消息
        // 3.1 创建响应对象的构建者
        HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
        // 3.2 向响应对象中填充响应数据
        builder.setResult("hello method invoke ok");
        // 3.3 封装响应
        HelloProto.HelloResponse helloResponse = builder.build();

        // 将响应返回给客户端
        responseObserver.onNext(helloResponse);
        responseObserver.onCompleted();
    }


    /**
     * 2.
     * @param request
     * @param responseObserver
     */
    @Override
    public void hello1(HelloProto.HelloRequest1 request, StreamObserver<HelloProto.HelloResponse1> responseObserver) {
        ProtocolStringList nameList = request.getNameList();
        for (String s : nameList) {
            System.out.println(s);
        }
        System.out.println("执行务功能");

        // 构建响应
        HelloProto.HelloResponse1.Builder builder = HelloProto.HelloResponse1.newBuilder();
        builder.setResult("ok");
        HelloProto.HelloResponse1 helloResponse1 = builder.build();
        responseObserver.onNext(helloResponse1);
        responseObserver.onCompleted();
    }


    /**
     * 3. 服务端流式响应
     * @param request
     * @param responseObserver
     */
    @Override
    public void c2ss(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver)  {

        // 1. 接受客户端参数
        String name = request.getName();
        // 2. 业务处理
        System.out.println("c2ss name = " +name);
        // 3. 根据业务处理的结果，响应客户端
        for (int i = 0; i < 9; i++) {
            HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
            builder.setResult("处理的结果：i = " +i);
            HelloProto.HelloResponse response = builder.build();
            responseObserver.onNext(response);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 告诉客户端，我们处理完成了
        responseObserver.onCompleted();
    }


    /**
     * 4. 客户端流式 rpc
     * @param responseObserver
     * @return
     */
    @Override
    public StreamObserver<HelloProto.HelloRequest> cs2s(StreamObserver<HelloProto.HelloResponse> responseObserver) {
        return new StreamObserver<HelloProto.HelloRequest>() {
            @Override
            public void onNext(HelloProto.HelloRequest helloRequest) {
                System.out.println("接收到了客户端发送的消息： " + helloRequest.getName());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                // 监听客户端所有的消息都发送到了服务器端,
                HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
                builder.setResult("this is result");
                HelloProto.HelloResponse helloResponse = builder.build();
                responseObserver.onNext(helloResponse);
                responseObserver.onCompleted();
            }
        };
    }


    /**
     * 5. 双向流式 rpc
     * @param responseObserver
     * @return
     */
    @Override
    public StreamObserver<HelloProto.HelloRequest> cs2ss(StreamObserver<HelloProto.HelloResponse> responseObserver) {
        return new StreamObserver<HelloProto.HelloRequest>() {
            @Override
            public void onNext(HelloProto.HelloRequest helloRequest) {
                System.out.println("接收到了client 提交的消息 ： " + helloRequest.getName());
                responseObserver.onNext(HelloProto.HelloResponse.newBuilder().setResult("response " + helloRequest.getName()+" result").build());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("接收到了客户端的所有消息");
                responseObserver.onCompleted();
            }
        };
    }
}
